package com.wei.duowan.bbs.ui.adapter;

import android.support.annotation.IntRange;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.ViewManager;
import android.widget.LinearLayout;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.wei.duowan.bbs.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：赵若位
 * 时间：2019/8/22 15:52
 * 联系方式：1070138445
 * 功能：
 */
public abstract class BaseDelegateAdapter<T> extends DelegateAdapter.Adapter<BaseViewHolder>
{
    private static final int EMPTY = 0x100;//无数据
    private static final int CONTENT = 0x101;//有数据

    protected AppCompatActivity mActivity;
    private int mLayoutID;
    private int mEmptyID;
    private boolean isEmpty;//是否显示空布局
    protected List<T> mList;



    /*构造器*/
    public BaseDelegateAdapter(@Nullable AppCompatActivity activity, @LayoutRes int layoutID)
    {
        if (activity == null)
        {
            throw new NullPointerException("The AppCompatActivity is empty!");
        }
        this.mActivity = activity;
        this.mLayoutID = layoutID;
        this.mEmptyID = R.layout.layout_empty;
        this.isEmpty = false;
        mList = new ArrayList<>();
    }

    /*RecyclerView绑定Adapter*/
    public void bindRecyclerView(@Nullable RecyclerView recyclerView)
    {
        if (recyclerView == null)
        {
            throw new NullPointerException("The Bind RecyclerView is Empty!");
        }
        /*关闭刷新动效*/
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        recyclerView.setAdapter(this);
    }


    /*更新内容中所有的数据*/
    public void setNewData(@Nullable List<T> list)
    {
        this.mList.clear();
        if (list != null && list.size() != 0)
        {
            this.mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    /*更新内容中所有的数据*/
    public void setNewData(@Nullable T t)
    {
        this.mList.clear();
        if (t != null)
        {
            this.mList.add(t);
        }
        notifyDataSetChanged();
    }



    /*插入一组数据*/
    public void setData(@Nullable List<T> list, @IntRange(from = 0) int position)
    {
        if (list != null && list.size() != 0)
        {
            int index = getInsertPosition(position);
            this.mList.addAll(index, list);
            notifyItemRangeInserted(index, list.size());
        }
    }

    /*插入一组数据*/
    public void setData(@Nullable List<T> list)
    {
        this.setData(list, mList.size());
    }

    /*插入一条数据*/
    public void setData(@Nullable T t, @IntRange(from = 0) int position)
    {
        if (t != null)
        {
            int index = getInsertPosition(position);
            this.mList.add(index, t);
            notifyItemRangeInserted(index, 1);
        }
    }

    /*插入一条数据*/
    public void setData(@Nullable T t)
    {
        this.setData(t, mList.size());
    }


    /*获取数据插入的位置*/
    private int getInsertPosition(@IntRange(from = 0) int position)
    {
        return (position < 0 || position > mList.size()) ? mList.size() : position;
    }

    /*设置LayoutHelper*/
    public LayoutHelper setLayoutHelper()
    {
        return new LinearLayoutHelper();
    }

    /*设置是否显示空布局页面*/
    public BaseDelegateAdapter setIsEmpty(Boolean empty)
    {
        this.isEmpty = empty;
        return this;
    }

    /*是否显示空数据Layout*/
    private boolean isDisplayEmpty()
    {
        return isEmpty && (mList == null || mList.isEmpty());
    }


    @Override
    public LayoutHelper onCreateLayoutHelper()
    {
        return setLayoutHelper();
    }

    @Override
    public int getItemViewType(int position)
    {
        if (isDisplayEmpty())
        {
            return EMPTY;
        } else
        {
            return CONTENT;
        }
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int type)
    {
        return BaseViewHolder.getViewHolder(mActivity, LayoutInflater.from(mActivity.getApplicationContext())
                .inflate(type == EMPTY ? mEmptyID : mLayoutID, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position)
    {
        if (getItemViewType(position) == CONTENT)
        {
            convert(holder, position, mList.get(position));
        }
    }

    @Override
    public int getItemCount()
    {
        return isDisplayEmpty() ? 1 : (mList == null ? 0 : mList.size());
    }


    protected abstract void convert(@Nullable BaseViewHolder holder, int position, @Nullable T t);
}
